package com.css.fxfzfxqh.modules.buildingIndividual.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.buildingIndividual.entity.BuildingIndividualDataVersion;
import com.css.fxfzfxqh.modules.buildingIndividual.param.PageQueryParam;
import com.css.fxfzfxqh.modules.buildingIndividual.service.BuildingIndividualDataVersionService;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/buildingIndividualDataVersion")
@Slf4j
public class BuildingIndividualDataVersionController {

    @Autowired
    private BuildingIndividualDataVersionService buildingIndividualDataVersionService;

    /**
     * 保存新版本
     * @return
     */
    @PostMapping("saveNewVersion")
    public RestResponse saveNewVersion(@RequestBody BuildingIndividualDataVersion buildingIndividualDataVersion) {
        try {
            return RestResponse.succeed( buildingIndividualDataVersionService.saveNewVersion(buildingIndividualDataVersion));
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "新增失败";
            }
            log.error(errormessage,e);
            return RestResponse.fail(errormessage);
        }
    }

    /**
     * 分页列表
     *
     * @param queParams
     * @return
     */
    @GetMapping("/page")
    public RestResponse getPage(PageQueryParam queParams, HttpServletRequest request) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = buildingIndividualDataVersionService.getPage(queParams, curPage, pageSize);
            restResponse = RestResponse.succeed(page);
            restResponse.setMessage("查询成功");
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public RestResponse delete(String id) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            buildingIndividualDataVersionService.delete(id);
            response = RestResponse.succeed();
            response.setMessage("删除成功");
        } catch (Exception e) {
            String errormessage = "删除失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }
}
